﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sungero.Core;
using Sungero.CoreEntities;
using Sungero.Domain.Initialization;

namespace akibank.Dossier.Server
{
  public partial class ModuleInitializer
  {

    public override void Initializing(Sungero.Domain.ModuleInitializingEventArgs e)
    {
      InitializationLogger.Debug("Создание типов документов для досье клиентов");
      CreateDocumentTypes(); 
      InitializationLogger.Debug("Создание вида документов для досье клиентов");
      CreateDocumentKinds();
      InitializationLogger.Debug("Выдача права на документы");
      GrantCreateRightsOnContractsDocument();
      InitializationLogger.Debug("Добавления метода доставки интернет банк");
      AddMailDeliveryMethod();
    }
    
    /// <summary>
    /// Создание типа документа "Кредитный договор" в служебном справочнике "Типы документов"
    /// </summary>
    public static void CreateDocumentTypes()
    {
      Sungero.Docflow.PublicInitializationFunctions.Module.CreateDocumentType("Договор досье клиента", DossierContract.ClassTypeGuid, Sungero.Docflow.DocumentType.DocumentFlow.Inner, false);
      Sungero.Docflow.PublicInitializationFunctions.Module.CreateDocumentType("Документ обмена досье клиента", DossierExchangeDocument.ClassTypeGuid, Sungero.Docflow.DocumentType.DocumentFlow.Incoming, true);
      Sungero.Docflow.PublicInitializationFunctions.Module.CreateDocumentType("Документ досье", DossierCommonDocument.ClassTypeGuid, Sungero.Docflow.DocumentType.DocumentFlow.Incoming, false);
    }

    /// <summary>
    /// Создание видов для документов кредитных договоров
    /// </summary>
    public static void CreateDocumentKinds()
    {
      Sungero.Docflow.PublicInitializationFunctions.Module.CreateDocumentKind("Кредитная заявка",
        "Кредитная заявка",
        Sungero.Docflow.DocumentKind.NumberingType.NotNumerable,
        Sungero.Docflow.DocumentType.DocumentFlow.Inner,
        true,
        false,
        DossierContract.ClassTypeGuid,
        new Sungero.Domain.Shared.IActionInfo[] { Sungero.Docflow.OfficialDocuments.Info.Actions.SendForApproval },
        Constants.Module.LoanApplicationKind);
      
      Sungero.Docflow.PublicInitializationFunctions.Module.CreateDocumentKind("Кредитный договор",
        "Кредитный договор",
        Sungero.Docflow.DocumentKind.NumberingType.NotNumerable,
        Sungero.Docflow.DocumentType.DocumentFlow.Inner,
        true,
        false,
        DossierContract.ClassTypeGuid,
        new Sungero.Domain.Shared.IActionInfo[] { Sungero.Docflow.OfficialDocuments.Info.Actions.SendForApproval },
        Constants.Module.LoanContractKind);
      
      Sungero.Docflow.PublicInitializationFunctions.Module.CreateDocumentKind("Договор залога",
        "Договор залога",
        Sungero.Docflow.DocumentKind.NumberingType.NotNumerable,
        Sungero.Docflow.DocumentType.DocumentFlow.Inner,
        true,
        false,
        DossierContract.ClassTypeGuid,
        new Sungero.Domain.Shared.IActionInfo[] { Sungero.Docflow.OfficialDocuments.Info.Actions.SendForApproval },
        Constants.Module.PledgeContractKind);
      
      Sungero.Docflow.PublicInitializationFunctions.Module.CreateDocumentKind("Документ обмена",
        "Документ обмена",
        Sungero.Docflow.DocumentKind.NumberingType.NotNumerable,
        Sungero.Docflow.DocumentType.DocumentFlow.Incoming,
        false,
        false,
        DossierExchangeDocument.ClassTypeGuid,
        new Sungero.Domain.Shared.IActionInfo[] { Sungero.Docflow.OfficialDocuments.Info.Actions.SendForApproval },
        Constants.Module.DossierExchangeDocumentKind);
      
    }
    
    /// <summary>
    /// Выдача прав на создание договоров досье клиента.
    /// </summary>
    public static void GrantCreateRightsOnContractsDocument()
    {
      //TODO: Изменить на спец. группу для работы с ЭДК
      var allUsers = Roles.AllUsers;
      DossierContracts.AccessRights.Grant(allUsers, DefaultAccessRightsTypes.Read);
      DossierContracts.AccessRights.Grant(allUsers, DefaultAccessRightsTypes.Create);
      DossierContracts.AccessRights.Save();
      
      DossierDocuments.AccessRights.Grant(allUsers, DefaultAccessRightsTypes.Read);
      DossierDocuments.AccessRights.Grant(allUsers, DefaultAccessRightsTypes.Create);
      DossierDocuments.AccessRights.Save();
    }

    /// <summary>
    /// 
    /// </summary>
    public static void AddMailDeliveryMethod()
    {
      string deliveryMethodSid = Dossier.Constants.Module.IbDeliveryMethod;
      var deliveryMethod = Sungero.Docflow.MailDeliveryMethods.GetAll(t => t.Sid.Equals(deliveryMethodSid)).FirstOrDefault();
      if (deliveryMethod == null)
      {
        deliveryMethod = Sungero.Docflow.MailDeliveryMethods.Create();
        deliveryMethod.Name = Dossier.Resources.DeliveryMethodName;
        deliveryMethod.Sid = deliveryMethodSid;
        deliveryMethod.Save();
      }
    }  
  }
}
